var resourceUrl = getApp().request("imgUrl");
var levelList = require("../../utils/level.js");
var viewH = null;
var viewW = null;
var app = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowH:"",
    windowW:""
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    wx.getSystemInfo({
      success: function(res) {
        viewH = res.windowHeight;
        viewW = res.windowWidth;
      },
    })
    //获取本地的昵称和头像
    var nickName = app.nickName;
    var avatarUrl = app.avatarUrl;
    var myRefitTxt = app.myRefitTxt;
    var score = app.score;
    //级别
    var levelArr = levelList.list;
    //console.log(score)
    //console.log(levelArr)
    var levelName = null;
    for (var i = 0; i < levelArr.length; i++) {
      if (score >= parseInt(levelArr[i].min) && score <= parseInt(levelArr[i].max)) {
        levelName = levelArr[i];
        break;
      }
    }
    //console.log(levelName)
    //车辆类型
    var carName =app.car_name;
    carName = carName == "" ? "SUV概念车" : carName;
    const ctx=wx.createCanvasContext("ctx");
    
    var storageImgArr1 = app.storageImgArr1;
    console.log(storageImgArr1)
    var storageImgArr2 = app.storageImgArr2;
    setTimeout(function () {
      ctx.drawImage(storageImgArr1.img1, 0, 0, viewW, viewH);
      ctx.drawImage(storageImgArr1.img2, viewW * 0.01, viewW * 0.02, viewW * 0.98, viewW * 1.432);
      ctx.save()
      ctx.beginPath()
      ctx.arc(viewW * 0.15, viewW * 0.18, viewW * 0.08, 0, 2 * Math.PI)
      ctx.clip()
      ctx.drawImage(storageImgArr1.img3, viewW * 0.07, viewW * 0.1, viewW * 0.16, viewW * 0.16);
      ctx.restore();
      ctx.beginPath()
      ctx.arc(viewW * 0.15, viewW * 0.18, viewW * 0.08, 0, 2 * Math.PI);
      ctx.setStrokeStyle("#0a485d");
      ctx.setLineWidth(3)
      ctx.stroke();
      ctx.setFontSize(viewW * 0.04);
      ctx.setFillStyle("white")
      ctx.fillText(nickName, viewW * 0.26, viewW * 0.2,viewW * 0.7)
      ctx.setFontSize(viewW * 0.03);
      ctx.fillText(myRefitTxt, viewW * 0.2, viewW * 0.4, viewW * 0.7);
      ctx.drawImage(storageImgArr1.img4, viewW * 0.11, viewW * 0.56, viewW * 0.04, viewW * 0.056);
      ctx.setFillStyle("orange")
      ctx.setFontSize(viewW * 0.035);
      ctx.fillText(levelName.name, viewW * 0.16, viewW * 0.6);
      ctx.setFillStyle("#0ac5ed")
      ctx.fillText(carName, viewW * 0.32, viewW * 0.6);
      ctx.drawImage(storageImgArr1.img5, viewW * 0.62, viewW * 0.57, viewW * 0.0501, viewW * 0.0347);
      ctx.setFillStyle("#d2d2d2")
      ctx.fillText("评分：" + score, viewW * 0.68, viewW * 0.6);
      var storageImgArr22 =[];
      if (storageImgArr2.c){
        storageImgArr22.push(storageImgArr2.c);
      }
      if (storageImgArr2.f) {
        storageImgArr22.push(storageImgArr2.f);
      }
      if (storageImgArr2.b) {
        storageImgArr22.push(storageImgArr2.b);
      }
      if (storageImgArr2.a) {
        storageImgArr22.push(storageImgArr2.a);
      }
      if (storageImgArr2.d) {
        storageImgArr22.push(storageImgArr2.d);
      }
      if (storageImgArr2.e) {
        storageImgArr22.push(storageImgArr2.e);
      }
      if (storageImgArr2.g) {
        storageImgArr22.push(storageImgArr2.g);
      }
      console.log(storageImgArr22)
      for (var i = 0; i < storageImgArr22.length;i++){
        ctx.drawImage(storageImgArr22[i], viewW * 0.13, viewW * 0.63, viewW * 0.74, viewW * 0.44);
      }
      // for (var val in storageImgArr2){
      //   ctx.drawImage(storageImgArr2[val], viewW * 0.13, viewW * 0.63, viewW * 0.74, viewW * 0.44);
      // }
      setTimeout(function(){
        ctx.draw();
        wx.hideLoading()
      },500)
      
    }, 500);
    
    //保存海报到手机
    const ctxSave = wx.createCanvasContext("savePoster");
    setTimeout(function () {
      console.log("画出来了没+++++")
      ctxSave.drawImage(app.btnSave, 0, 0, viewW * 0.94, viewW * 0.113);
      ctxSave.draw();
    }, 600)
    
    //返回按钮
    const back = wx.createCanvasContext("btnBack");
    setTimeout(function () {
      back.drawImage(app.btnBack, 0, 0, viewW * 0.222, viewW * 0.084);
      back.setFontSize(viewW * 0.04);
      back.setFillStyle("white")
      back.fillText("返回", viewW * 0.08, viewW * 0.055)
      back.draw();
    }, 800)
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    

    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showShareMenu({
      withShareTicket: true
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    //是否选车
    var fightCarTypeId = app.fightCarTypeId;
    //随机一句宣言
    var declaration = getApp().Refit();
    var storageRefit = app.myRefitTxt;
    var myRefitTxt = declaration.indexOf(storageRefit);
    console.log(myRefitTxt)
    
    if (fightCarTypeId == 0) {
      return {
        title: getApp().shareTitle1(),
        path: "/pages/index/index"
      }
    } else {
      return {
        title: getApp().shareTitle2(),
        path: "/pages/openShare/openShare?shareId=" + app.session_key.userId + "&carTypeId=" + fightCarTypeId + "&shareRefitNum=" + myRefitTxt + "&carName=SUV概念车",
        success: (re) => {
          if (Object.keys(re).length == 1) {
            return
          }
          var ticket = re.shareTickets[0];
          getApp().shareFn(ticket);
        }
      }
    }
  },
  btnSavePoster:function(){
    var ctx = wx.createCanvasContext("ctx");
    wx.canvasToTempFilePath({
      x: viewW * 0.03,
      y: viewW * 0.04,
      width: viewW*0.94,
      height: viewW*1.38,
      canvasId: 'ctx',
      success:function(res){
        console.log(res.tempFilePath);
        var saveImgUrl = res.tempFilePath;
        wx.saveImageToPhotosAlbum({
          filePath: saveImgUrl,
          success:function(){
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            })
          }
        })
      }
    })
  },
  btnBack(){
    wx.navigateBack({
      delta:1
    })
  }
})